//////////Change the directory before running the code
///////// Summary stats for characteristics
cd "D:\data&code"
quietly {

	clear
	tempfile collector
	save `collector', emptyok
	
	use pseudo_dataset/targeting, clear
	
	foreach this_var of varlist mv q roa stock_return_adj leverage divyld inst_13f mf_share hhi {
	
		//////// Voted only
	
		//// Load file
		use pseudo_dataset/targeting, clear
		keep if contest == 1 & contest_year == 1
		
		replace mv_crsp = mv_crsp/1000000000
		keep `this_var'
		
		//// Stats
		egen var_mean = mean(`this_var')
		egen var_med  = median(`this_var')
		egen var_sd   = sd(`this_var')
		drop `this_var'
		
		//// Collect
		keep if _n == 1
		gen covariate = "`this_var'"
		gen group = "voted"
		
		append using `collector'
		save `collector', replace
	
		//////// Settled
	
		//// Load file
		use pseudo_dataset/targeting, clear
		keep if contest == 2 & contest_year == 1
		
		replace mv_crsp = mv_crsp/1000000000
		keep `this_var'
		
		//// Stats
		egen var_mean = mean(`this_var')
		egen var_med  = median(`this_var')
		egen var_sd   = sd(`this_var')
		drop `this_var'
		
		//// Collect
		keep if _n == 1
		gen covariate = "`this_var'"
		gen group = "settled"
		
		append using `collector'
		save `collector', replace	
	
		//////// Withdrawn
	
		//// Load file
		use pseudo_dataset/targeting, clear
		keep if contest == 3 & contest_year == 1
		
		replace mv_crsp = mv_crsp/1000000000
		keep `this_var'
		
		//// Stats
		egen var_mean = mean(`this_var')
		egen var_med  = median(`this_var')
		egen var_sd   = sd(`this_var')
		drop `this_var'
		
		//// Collect
		keep if _n == 1
		gen covariate = "`this_var'"
		gen group = "withdrawn"
		
		append using `collector'
		save `collector', replace	
	
	}
	
	///// Reshape
	order covariate group
	reshape wide var_*, i(covariate) j(group) string
	
	rename var_meanvoted mean_voted
	rename var_medvoted  med_voted
	rename var_sdvoted   sd_voted
	rename var_meansettled mean_settled
	rename var_medsettled  med_settled
	rename var_sdsettled  sd_settled
	rename var_meanwithdrawn mean_withdrawn
	rename var_medwithdrawn  med_withdrawn
	rename var_sdwithdrawn  sd_withdrawn
	
	////// Clean up
	order covariate *_voted *_settled *_withdrawn
	
	gen rank = .
	replace rank = 1 if covariate == "mv_crsp"
	replace rank = 2 if covariate == "q_CCM"
	replace rank = 3 if covariate == "roa_CCM"
	replace rank = 4 if covariate == "stock_return_adj"
	replace rank = 5 if covariate == "leverage_CCM"
	replace rank = 6 if covariate == "divyld_CCM"
	replace rank = 7 if covariate == "inst_13f"
	replace rank = 8 if covariate == "mf_share"
	replace rank = 9 if covariate == "hhi"
	
	sort rank
	drop rank
	
	replace covariate = "mv" if covariate == "mv_crsp"
	replace covariate = "q"  if covariate == "q_CCM"
	replace covariate = "roa" if covariate == "roa_CCM"
	replace covariate = "leverage" if covariate == "leverage_CCM"
	replace covariate = "divyld" if covariate == "divyld_CCM"
	
	/////// Save
	tempfile table_2_ss
	save `table_2_ss', replace
}

/////////////////////// Closest match to controls

//////////////// Set up controls file
quietly {	
	
	use pseudo_dataset/targeting, clear
	keep if contest == 0

	keep gvkey proxy_year sic_CCM mv q roa stock_return_adj leverage divyld inst_13f mf_share
	rename mv_crsp mv
	rename *_CCM *

	foreach var of varlist gvkey mv q roa stock_return_adj leverage divyld inst_13f mf_share {	
		rename `var' control_`var'	
	}

	tempfile controls
	save `controls', replace

}

/////////////// Match voted events

quietly {

	//// Set up contests file
	use pseudo_dataset/targeting, clear
	keep if contest == 1 & contest_year == 1

	keep gvkey pf_no proxy_year sic_CCM mv_crsp q roa stock_return_adj leverage divyld inst_13f mf_share
	rename mv_crsp mv
	rename *_CCM *

	rename pf_no contest_id

	tempfile voted
	save `voted', replace
		 
	levelsof contest_id, local(contest_ids)
		
	//// Iterate
	clear

	tempfile collector
	save `collector', emptyok replace

	foreach contest_id of local contest_ids {

		//// Load contests file
		use `voted', clear
		save `voted', replace
		keep if contest_id == `contest_id'
		
		//// Merge in controls
		merge 1:m proxy_year sic using `controls'
		keep if _merge == 3
		drop _merge
		
		//// Identify control firm closest in market cap
		gen mv_diff = abs(mv - control_mv)
		sort mv_diff control_gvkey
		keep if _n == 1
		drop mv_diff

		//// Add to collector
		append using `collector'
		save `collector', replace

	} 

	///// Compute differences
	foreach var of varlist q roa stock_return_adj leverage divyld inst_13f mf_share {
		gen `var'_diff = .
		replace `var'_diff = `var' - control_`var' if `var' != . & control_`var' != .
	}

	keep *_diff

	tempfile voted_diff
	save `voted_diff', replace

	////////// t-test
	clear
	tempfile collector
	save `collector', emptyok replace

	use `voted_diff', clear
	save `voted_diff', replace


	foreach this_var of varlist q_diff roa_diff stock_return_adj_diff leverage_diff divyld_diff inst_13f_diff mf_share_diff {

		// Load file
		use `voted_diff', clear
		save `voted_diff', replace

		// Keep this variable
		keep `this_var'
		drop if `this_var' == .
		
		// t-stat
		egen mean_`this_var' = mean(`this_var')
		egen sd_`this_var'   = sd(`this_var')
		gen se_`this_var' = sd_`this_var'/sqrt(_N)
		gen t_`this_var' = mean_`this_var'/se_`this_var'
		
		// Clean
		keep if _n == 1
		keep mean_`this_var' t_`this_var'
		
		keep mean_`this_var' t_`this_var'
		
		rename mean_`this_var' mean_voted
		rename t_`this_var'    t_voted
		
		gen covariate = "`this_var'"
		
		// Collect
		append using `collector'
		save `collector', replace

	}
	
	order covariate mean_voted t_voted
	
	tempfile voted_diff
	save `voted_diff', replace

}


/////////////// Match settled events

quietly {

	//// Set up contests file
	use pseudo_dataset/targeting, clear
	keep if contest == 2 & contest_year == 1

	keep gvkey pf_no proxy_year sic_CCM mv_crsp q roa stock_return_adj leverage divyld inst_13f mf_share
	rename mv_crsp mv
	rename *_CCM *

	rename pf_no contest_id

	tempfile settled
	save `settled', replace
		 
	levelsof contest_id, local(contest_ids)
		
	//// Iterate
	clear

	tempfile collector
	save `collector', emptyok replace

	foreach contest_id of local contest_ids {

		//// Load contests file
		use `settled', clear
		save `settled', replace
		keep if contest_id == `contest_id'
		
		//// Merge in controls
		merge 1:m proxy_year sic using `controls'
		keep if _merge == 3
		drop _merge
		
		//// Identify control firm closest in market cap
		gen mv_diff = abs(mv - control_mv)
		sort mv_diff control_gvkey
		keep if _n == 1
		drop mv_diff

		//// Add to collector
		append using `collector'
		save `collector', replace

	} 


	///// Compute differences
	foreach var of varlist q roa stock_return_adj leverage divyld inst_13f mf_share {
		gen `var'_diff = .
		replace `var'_diff = `var' - control_`var' if `var' != . & control_`var' != .
	}

	keep *_diff

	tempfile settled_diff
	save `settled_diff', replace
	
	////////// t-test
	clear
	tempfile collector
	save `collector', emptyok replace

	use `settled_diff', clear
	save `settled_diff', replace

	foreach this_var of varlist q_diff roa_diff stock_return_adj_diff leverage_diff divyld_diff inst_13f_diff mf_share_diff {

		// Load file
		use `settled_diff', clear
		save `settled_diff', replace

		// Keep this variable
		keep `this_var'
		drop if `this_var' == .
		
		// t-stat
		egen mean_`this_var' = mean(`this_var')
		egen sd_`this_var'   = sd(`this_var')
		gen se_`this_var' = sd_`this_var'/sqrt(_N)
		gen t_`this_var' = mean_`this_var'/se_`this_var'
		
		// Clean
		keep if _n == 1
		keep mean_`this_var' t_`this_var'
		
		keep mean_`this_var' t_`this_var'
		
		rename mean_`this_var' mean_settled
		rename t_`this_var'    t_settled
		
		gen covariate = "`this_var'"
		
		// Collect
		append using `collector'
		save `collector', replace

	}
	
	order covariate mean_settled t_settled
	
	tempfile settled_diff
	save `settled_diff', replace

}


/////////////// Match withdrawn events

quietly {

	//// Set up contests file
	use pseudo_dataset/targeting, clear
	keep if contest == 3 & contest_year == 1

	keep gvkey pf_no proxy_year sic_CCM mv_crsp q roa stock_return_adj leverage divyld inst_13f mf_share
	rename mv_crsp mv
	rename *_CCM *

	rename pf_no contest_id

	tempfile withdrawn
	save `withdrawn', replace
		 
	levelsof contest_id, local(contest_ids)
		
	//// Iterate
	clear

	tempfile collector
	save `collector', emptyok replace

	foreach contest_id of local contest_ids {

		//// Load contests file
		use `withdrawn', clear
		save `withdrawn', replace
		keep if contest_id == `contest_id'
		
		//// Merge in controls
		merge 1:m proxy_year sic using `controls'
		keep if _merge == 3
		drop _merge
		
		//// Identify control firm closest in market cap
		gen mv_diff = abs(mv - control_mv)
		sort mv_diff control_gvkey
		keep if _n == 1
		drop mv_diff

		//// Add to collector
		append using `collector'
		save `collector', replace

	} 


	///// Compute differences
	foreach var of varlist q roa stock_return_adj leverage divyld inst_13f mf_share {
		gen `var'_diff = .
		replace `var'_diff = `var' - control_`var' if `var' != . & control_`var' != .
	}

	keep *_diff

	tempfile withdrawn_diff
	save `withdrawn_diff', replace
	
	////////// t-test
	clear
	tempfile collector
	save `collector', emptyok replace

	use `withdrawn_diff', clear
	save `withdrawn_diff', replace

	foreach this_var of varlist q_diff roa_diff stock_return_adj_diff leverage_diff divyld_diff inst_13f_diff mf_share_diff {

		// Load file
		use `withdrawn_diff', clear
		save `withdrawn_diff', replace

		// Keep this variable
		keep `this_var'
		drop if `this_var' == .
		
		// t-stat
		egen mean_`this_var' = mean(`this_var')
		egen sd_`this_var'   = sd(`this_var')
		gen se_`this_var' = sd_`this_var'/sqrt(_N)
		gen t_`this_var' = mean_`this_var'/se_`this_var'
		
		// Clean
		keep if _n == 1
		keep mean_`this_var' t_`this_var'
		
		keep mean_`this_var' t_`this_var'
		
		rename mean_`this_var' mean_withdrawn
		rename t_`this_var'    t_withdrawn
		
		gen covariate = "`this_var'"
		
		// Collect
		append using `collector'
		save `collector', replace

	}
	
	order covariate mean_withdrawn t_withdrawn
	
	tempfile withdrawn_diff
	save `withdrawn_diff', replace

}


//////////////// Merge the three together
merge 1:1 covariate using `voted_diff'
drop _merge

merge 1:1 covariate using `settled_diff'
drop _merge

order covariate mean_voted t_voted mean_settled t_settled mean_withdrawn t_withdrawn
rename mean_voted     diff_voted
rename mean_settled   diff_settled
rename mean_withdrawn diff_withdrawn

replace covariate = "q" 				if covariate == "q_diff"
replace covariate = "roa" 				if covariate == "roa_diff"
replace covariate = "stock_return_adj" 	if covariate == "stock_return_adj_diff"
replace covariate = "leverage" 			if covariate == "leverage_diff"
replace covariate = "divyld" 			if covariate == "divyld_diff"
replace covariate = "inst_13f" 			if covariate == "inst_13f_diff"
replace covariate = "mf_share" 			if covariate == "mf_share_diff"

///// Save
tempfile table_2_diff
save `table_2_diff', replace

//////// Merge the two together
use `table_2_ss', clear

merge 1:1 covariate using `table_2_diff'
drop _merge

gen rank = .
replace rank = 1 if covariate == "mv"
replace rank = 2 if covariate == "q"
replace rank = 3 if covariate == "roa"
replace rank = 4 if covariate == "stock_return_adj"
replace rank = 5 if covariate == "leverage"
replace rank = 6 if covariate == "divyld"
replace rank = 7 if covariate == "inst_13f"
replace rank = 8 if covariate == "mf_share"
replace rank = 9 if covariate == "hhi"

sort rank
drop rank

// Order columns as in table 
order covariate mean_voted     med_voted     sd_voted      diff_voted     t_voted    ///
                mean_settled   med_settled   sd_settled   diff_settled   t_settled  ///
				mean_withdrawn med_withdrawn sd_withdrawn diff_withdrawn t_withdrawn
